package reflection;

import java.lang.reflect.Field;

public class ReflectionTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ReflectionTest rt = new ReflectionTest();
		Students s = new Students();
		s.setId("001");
		s.setName("hello");
		System.out.println(rt.getProperty(s, "name"));
	}

	public Object getProperty(Object owner, String fieldName) throws Exception {       
		//返回此 Object 的运行时类
		Class ownerClass = owner.getClass();
		
		//返回一个 Field 对象，它反映此 Class 对象所表示的类或接口的指定公共成员字段
		Field field = ownerClass.getField(fieldName);
		
		//返回指定对象上此 Field 表示的字段的值
		Object property = field.get(owner);
		return property;                                                               
	}      
	
}
